#define PC

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

#if PC
using System.Diagnostics;
#endif

namespace TimeWar
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameCulture : Microsoft.Xna.Framework.GameComponent
    {

        private Cultures culture;
        private Game timeWarGameObj = null;
        private Texture2D sideGraphic = null;
        private SpriteFont cultureFont = null;
        private Vector2 sideGraphicPosition;
        private Vector2 cultureFontPosition;
        private int screenwidth, screenheight;
        protected SpriteBatch batch = null;
        private Rectangle sideGraphicArea;
        private bool isSomethingOver = false;
        private GameCursor cursor = null;

        public GameCulture(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            timeWarGameObj = game;
        }

        public GameCulture(Game game, SpriteBatch spriteBatch, float posX, float posY, Cultures cultureName, string pathToTexture, string pathToFont, int currScreenWidth, int currScreenHeight)
            : base(game)
        {
            timeWarGameObj = game;
            batch = spriteBatch;
            culture = cultureName;
            screenwidth = currScreenWidth;
            screenheight = currScreenHeight;
            LoadCultureGraphic(pathToTexture);
            LoadCultureFont(pathToFont);
            setSideGraphicPosition(posX, posY);
        }


        public void setScreenSize(int width, int height)
        {
            screenheight = height;
            screenwidth = width;
        }

        public void setSpriteBatch(SpriteBatch value)
        {
            batch = value;
        }

        public GameCursor Cursor
        {
            set
            {
                cursor = value;
            }
        }

        public void setSideGraphicPosition(float X, float Y)
        {
            float calculatedX = 0;
            float calculatedY = 0;

            if (X < 0)
            {
                calculatedX = screenwidth - sideGraphic.Width;

            }

            if (Y < 0)
            {
                //calculatedY = screenheight - (sideGraphic.Height / 2);
                calculatedY = 0;
            }

            sideGraphicPosition = new Vector2(calculatedX, calculatedY);
            sideGraphicArea = new Rectangle(Convert.ToInt32(calculatedX), Convert.ToInt32(calculatedY), sideGraphic.Width, sideGraphic.Height);
            calculatedX = (sideGraphicArea.Left + sideGraphicArea.Right) / 2;
            calculatedY = (sideGraphicArea.Top + sideGraphicArea.Bottom) / 2;
            cultureFontPosition = new Vector2(calculatedX, calculatedY);
        }




        public string Name
        {
            get
            {
                if (culture == Cultures.Daleks)
                {
                    return "Daleks";
                }
                else if (culture == Cultures.TimeLords)
                {
                    return "Time Lords";
                }
                else
                {
                    return "No culture";
                }
            }
        }

        public int Width
        {
            get
            {
                return sideGraphic.Width;
            }
        }

        public int Height
        {
            get
            {
                return sideGraphic.Height;
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public bool isOver(Vector2 cursorPosition)
        {

            //origin coordinate, (0, 0), is top left hand corner of display

            if (cursorPosition.X < sideGraphicArea.Right && cursorPosition.X > sideGraphicArea.Left)
            {
                if (cursorPosition.Y > sideGraphicArea.Top && cursorPosition.Y < sideGraphicArea.Bottom)
                {
                    isSomethingOver = true;
                    return true;
                }
            }

            //if (cursorPosition.X < sideGraphicArea.Right && cursorPosition.X > sideGraphicArea.Left)
            //{
            //    isSomethingOver = true;
            //    return true;
            //}

            isSomethingOver = false;
            return false;

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            //Draw();

            //check position of cursor before determining texture to draw

            base.Update(gameTime);
        }

        public void Draw()
        {
            if (isSomethingOver)
            {
                //batch.Draw(sideGraphic, sideGraphicPosition, Color.LightGreen);
                batch.Draw(sideGraphic, new Rectangle(Convert.ToInt32(sideGraphicPosition.X), Convert.ToInt32(sideGraphicPosition.Y), sideGraphic.Width, sideGraphic.Height), null, Color.LightGreen, 0, new Vector2(0, 0), SpriteEffects.None, 1);
#if PC
                Trace.WriteLine("[" + DateTime.Now + "]: Cursor over game culture: " + this.Name);
#endif

            }
            else
            {
                //batch.Draw(sideGraphic, sideGraphicPosition, Color.White);
                batch.Draw(sideGraphic, new Rectangle(Convert.ToInt32(sideGraphicPosition.X), Convert.ToInt32(sideGraphicPosition.Y), sideGraphic.Width, sideGraphic.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 1);
#if PC
                Trace.WriteLine("[" + DateTime.Now + "]: Cursor not over culture.");
#endif

            }

            // Find the center of the string
            Vector2 cultureFontOrigin = cultureFont.MeasureString(this.Name) / 2;

            batch.DrawString(cultureFont, this.Name, cultureFontPosition, Color.Tomato, 0, cultureFontOrigin, 1.0f, SpriteEffects.None, 1.0f);

            if (cursor != null)
            {
                //cursor.Draw();
            }



        }

        public void LoadCultureGraphic(string texturePath)
        {
            //sideGraphic = timeWarGameObj.Content.Load<Texture2D>(texturePath);
            sideGraphic = TimeWarGame.ContentManager.Load<Texture2D>(texturePath);

        }

        public void LoadCultureFont(string fontPath)
        {
            //cultureFont = timeWarGameObj.Content.Load<SpriteFont>(fontPath);
            cultureFont = TimeWarGame.ContentManager.Load<SpriteFont>(fontPath);

        }

    }
}